define(["jquery","layuiall","zTree","jqform"],function ($,layui,zTree,jqform) {
    var form = layui.form;
    controller = {
        index:function () {
            jqform.delete()

            //数据全部选中
            form.on('checkbox(select-all)', function(data){

                if(data.elem.checked==true){
                    $("input[name='idList[]']").prop('checked',true)
                }else {
                    $("input[name='idList[]']").prop('checked',false)
                }
                form.render('checkbox');
            });
        },
        adduserapp:function () {
            $.ajax({
                type:"get",
                url :"/admin/openservice.userapp/adduserapp",
                dataType:'json',
                data:{
                    dataType:"Apps"
                },
                success:function (res) {
                    var zTreeObj;
                    var setting = {
                        view: {
                            showIcon: true
                        },
                        data: {
                            simpleData: {
                                enable: true,
                                idKey:"id",
                                pIdKey:"parent_id",
                            },
                            key:{
                                name:"title"
                            }
                        },
                        check: {
                            enable: true,
                            chkStyle: "radio",
                            radioType: "all"
                        },
                        callback: {
                            onClick: function (e, treeId, treeNode, clickFlag) {
                                zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                nodes = zTreeObj.getCheckedNodes(true)
                                var id = ''
                                for (var i = 0; i < nodes.length; i++) {
                                    id = nodes[i].id
                                    getFreeAppsApi(id,'/admin/openservice.userapp/adduserapp')
                                }
                                $("input[name='group_id']").val(id)
                            },
                            onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                $("input[name='group_id']").val(treeNode.id)
                                getFreeAppsApi(treeNode.id,'/admin/openservice.userapp/adduserapp')
                            },
                        }
                    };
                    zTreeObj = $.fn.zTree.init($("#Apps"), setting, res);
                    zTreeObj.expandAll(true);
                }
            })
            jqform.init()
        },
        edituserapp:function () {
            var appkey = $("#Apps").attr("appkey");
            $.ajax({
                type:"get",
                url :"/admin/openservice.userapp/edituserapp",
                dataType:'json',
                data:{
                    dataType:"Apps",
                    appkey:appkey
                },
                success:function (res) {
                    var zTreeObj;
                    var setting = {
                        view: {
                            showIcon: true
                        },
                        data: {
                            simpleData: {
                                enable: true,
                                idKey:"id",
                                pIdKey:"parent_id",
                            },
                            key:{
                                name:"title"
                            }
                        },
                        check: {
                            enable: true,
                            chkStyle: "radio",
                            radioType: "all"
                        },
                        callback: {
                            onClick: function (e, treeId, treeNode, clickFlag) {
                                zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                                nodes = zTreeObj.getCheckedNodes(true)
                                var id = ''
                                for (var i = 0; i < nodes.length; i++) {
                                    id = nodes[i].id
                                    getFreeAppsApi(id)
                                }
                                $("input[name='group_id']").val(id)
                            },
                            onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                                $("input[name='group_id']").val(treeNode.id)
                                getFreeAppsApi(treeNode.id)
                            },
                        }
                    };
                    zTreeObj = $.fn.zTree.init($("#Apps"), setting, res);
                    zTreeObj.expandAll(true);
                    getFreeAppsApi(appkey,'/admin/openservice.userapp/edituserapp')
                }
            })
            jqform.init()
        }
    }

    //获取收费的api 配置
    function getFreeAppsApi(app_id,url) {
        var rule_id = [];
        $.ajax({
            type:"get",
            url :url,
            dataType:'json',
            data:{
                app_id:app_id
            },
            success:function (res) {
                var zTreeObj;
                var setting = {
                    view: {
                        showIcon: true
                    },
                    data: {
                        simpleData: {
                            enable: true,
                            idKey:"id",
                            pIdKey:"parent_id",
                        },
                        key:{
                            name:"title"
                        }
                    },
                    check: {
                        enable: true,
                        chkStyle: "checkbox",
                        radioType: "all"
                    },
                    callback: {
                        onClick: function (e, treeId, treeNode, clickFlag) {
                            zTreeObj.checkNode(treeNode, !treeNode.checked, true);
                            nodes = zTreeObj.getCheckedNodes(true)
                            for (var i = 0; i < nodes.length; i++) {
                                rule_id.push(nodes[i].id)
                            }
                            $("input[name='free_api']").val(rule_id.splice(','))
                        },
                        onCheck:function zTreeOnCheck(event, treeId, treeNode) {
                            nodes = zTreeObj.getCheckedNodes(true)
                            for (var i = 0; i < nodes.length; i++) {
                                rule_id.push(nodes[i].id)
                            }
                            $("input[name='free_api']").val(rule_id.splice(','))
                        }
                    }
                };
                zTreeObj = $.fn.zTree.init($("#AppApi"), setting, res);
                zTreeObj.expandAll(true);
            }
        })
    }
    return controller;
})